package demo1;

class Solution {

    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k %= n;
        reverse(nums, 0, n-1);
        reverse(nums, 0, k-1);
        reverse(nums, k, n -1);
    }

    public void reverse(int[] nums, int i, int j) {
        while (i < j) {
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
            i++;
            j--;
        }
    }
    public int j = 0;
    public int removeElement(int[] nums, int val) {
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[j] = nums[i];
                j++;
            }
        }
        return j;
    }


    public int j1 = 1;
    public int removeDuplicates(int[] nums) {
        for (int i = 1; i < nums.length; i++) {

            if (nums[i-1] != nums[i]) {
                nums[j1] = nums[i];
                j1++;
            }
        }
        return j1;
    }


    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int end = m + n -1;
        int end1 = m - 1;
        int end2 = n - 1;

        while (end1 >=0 && end2 >=0 ) {
            if (nums1[end1] < nums2[end2]) {
                nums1[end--] = nums2[end2--];
            }else {
                nums1[end--] = nums1[end1--];
            }
        }
        while(end1 >= 0) {
            nums1[end--] = nums1[end1--];
        }
        while(end2 >= 0) {
            nums1[end--] = nums2[end2--];
        }
    }
}